//
//  HomeTableViewController.swift
//  sinaweibo
//
//  Created by icefs2 on 15/11/8.
//  Copyright © 2015年 icefs2. All rights reserved.
//

import UIKit
import AFNetworking
import SVProgressHUD
//定义首页标示符
private let HomeCellId = "HomeCellId"
class HomeTableViewController: BaseTableviewControll {
    //数据源,模型数组
    private lazy var statues = [Status]()

    override func viewDidLoad() {
        super.viewDidLoad()
        if !userLogin {
        vistorloginview?.setupinfor("visitordiscover_feed_image_house", message: "登录后，最新、最热微博尽在掌握，不再会与实事潮流擦肩而过", ishome: true)
        }
        //准备表格
        preparedtableview()
        //加载数据
        loaddata { (array) -> () in
            if array == nil {
                SVProgressHUD.showInfoWithStatus(AppErrorTip)
            }
            //程序走到这里一定拿到数据
            self.statues = array!
            //刷新表格
            self.tableView.reloadData()
        }
    }
    private func preparedtableview() {
        //1.注册cell
        tableView.registerClass(StatuesCell.self, forCellReuseIdentifier: HomeCellId)
        //预估行高
      //  tableView.rowHeight = 200
        tableView.estimatedRowHeight = 200
        tableView.rowHeight = UITableViewAutomaticDimension
        }
    //加载网络数据
    private func loaddata(finised: (array: [Status]?) -> ()) {
    let AFN = AFHTTPSessionManager()
        let usrString = "https://api.weibo.com/2/statuses/home_timeline.json"
        guard let token = UserAccoutviewModel().token else {
            SVProgressHUD.showInfoWithStatus("您暂未登陆")
            return
        }
        let parameters = ["access_token" : token]
        AFN.GET(usrString, parameters: parameters, success: { (_, result) -> Void in
           
            guard let dict = result as? [String : AnyObject] else{
                //完成回调
                finised(array: nil)
                return
            }
            // 网络获取result是一个字典
//            print(result)

            if let array = dict["statuses"] as? [[String : AnyObject]]
            {
                print(array.count)
                //创建一个裂变数组
                var list = [Status]()
                //遍历数组
                for item in array {
                    //字典转模型
                    let s = Status(dict: item)
                    list.append(s)
                }
                //完成回调
                finised(array: list)
            }
            
            
            }) { (_, error) -> Void in
                print(error)
                //完成回调
                finised(array: nil)
        }
        
    }

    }

//包含了所有数据源方法和协议方法
extension HomeTableViewController {
    override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return statues.count
    }
    override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCellWithIdentifier(HomeCellId, forIndexPath: indexPath) as! StatuesCell
            cell.status = statues[indexPath.row]
        return cell
    }
    
}